import { defineStore } from 'pinia';
import axios from 'axios';
export const user = defineStore('user', {
	state: () => {
		return {
			// name: '李浩明',
			// age: 23,
			manageConfirm: false,
		};
	},
	getters: {
		// changeAge() {
		// 	return this.age + 1;
		// },
	},
	actions: {
		async judgeExist(usn) {
			try {
				const res = await axios({
					baseURL: 'http://localhost:5000',
					url: '/exist',
					method: 'post',
					data: {
						username: usn,
					},
				});
				return res.data.state;
			} catch (error) {
				console.log(error);
				return 'error';
			}
		},
		async validate(usn, pwd) {
			try {
				if (usn && pwd) {
					const res = await axios({
						baseURL: 'http://localhost:5000',
						url: '/login',
						method: 'post',
						data: {
							state: 'validate',
							username: usn,
							password: pwd,
						},
					});
					return res.data.state;
				} else {
					return 'error';
				}
			} catch (error) {
				console.log(error);
				return 'error';
			}
		},
		async getSpecifyUserMsg(usn, pwd) {
			if (usn && pwd) {
				try {
					const res = await axios({
						baseURL: 'http://localhost:5000',
						url: '/getUserMsg',
						method: 'post',
						data: {
							state: 'singleUser',
							username: usn,
							password: pwd,
						},
					});
					if (res.data.state === 'error') {
						return 'error';
					} else if (res.data.state === 'none') {
						return 'none';
					}
					return res.data.data;
				} catch (error) {
					console.log(error);
					return 'error';
				}
			} else if (usn) {
				try {
					const res = await axios({
						baseURL: 'http://localhost:5000',
						url: '/getUserMsg',
						method: 'post',
						data: {
							state: 'basedUsername',
							username: usn,
						},
					});
					if (res.data.state === 'ok') {
						return res.data.data;
					} else {
						return 'error';
					}
				} catch (error) {
					console.log(error);
					return 'error';
				}
			} else {
				return 'error';
			}
		},
	},
});
